package com.demo.mybatis;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.demo.mybatis.entity.DBConnectionEntity;
import com.demo.mybatis.entity.XMLBean;

/**
 * 
* @ClassName: CConfigeration
* @Description: 读取配置文件
* @author chenhongzhou
* @date 2018-4-2 下午9:57:34
*
 */
public class CConfigeration {
	private CConfigeration(){}
	
	private final static List<XMLBean> mapperList = loadMapperXML();
	private final static DBConnectionEntity dbConnection = loadConfigXML();
	
	/**
	 * 
	* @Title: loadMapperXML
	* @author: 陈泓州
	* @Description: 读mapper配置--模拟mapper.xml
	* @return List<XMLBean>    
	*
	 */
	public static List<XMLBean> loadMapperXML(){
		List<XMLBean> list = new ArrayList<XMLBean>();
		Map<String,String> map = new HashMap<String,String>();
		try {
			
			Properties pro = new Properties();
			FileInputStream in = new FileInputStream("src/main/resources/mapper/PeopleMapper.properties");
			pro.load(in);
			String namespace = (String) pro.get("namespace");
			String med = (String) pro.get("method");
			String sql = (String) pro.get("sql");
			
			map.put(med, sql);
			
			list.add(new XMLBean(namespace, map ));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	
	/**
	 * 
	* @Title: loadConfigXML
	* @author: 陈泓州
	* @Description: 读取数据库链接信息
	* @return DBConnectionEntity    
	*
	 */
	public static DBConnectionEntity loadConfigXML(){
		DBConnectionEntity db = new DBConnectionEntity();
		Map<String,String> map = new HashMap<String,String>();
		try {
			
			Properties pro = new Properties();
			FileInputStream in = new FileInputStream("src/main/resources/jdbc.properties");
			pro.load(in);
			String driver = (String) pro.get("jdbc_driverClassName");
			String url = (String) pro.get("jdbc_url");
			String username = (String) pro.get("jdbc_username");
			String password = (String) pro.get("jdbc_password");
			
			db.setDriver(driver);
			db.setUrl(url);
			db.setUsername(username);
			db.setPassword(password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return db;
	}
}
